/**
 * FacebookClientFactory.java
 * 
 * Created on Nov 9, 2007 at 2:09:18 AM
 */
package mobiled.bascule.facebook.client.impl;

import mobiled.bascule.facebook.FacebookAppConfig;
import mobiled.bascule.facebook.client.IFacebookFacade;
import mobiled.bascule.facebook.client.IFacebookFacadeFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.facebook.api.FacebookJsonRestClient;
import com.facebook.api.FacebookRestClient;

/**
 * Implementation that delegates to the Facebook Java API Library.
 * 
 * REMEMBER deal with Facebook exceptions. 
 * 
 * @author <a href="alexei.guevara@gmail.com">Alexei Guevara</>
 * @version $Revision:$ updated on $Date:$ by $Author:$
 */
@Component
public class FacebookFacadeFactory implements IFacebookFacadeFactory {
    
    private final FacebookAppConfig myFbAppConfig;

    @Autowired
    public FacebookFacadeFactory(FacebookAppConfig theFbAppConfig) {
	myFbAppConfig = theFbAppConfig;
    }

    public IFacebookFacade getClientUsingSessionKey(String theSessionKey) {
	return new FacebookFacadeRestClient( 
		new FacebookRestClient( myFbAppConfig.getApiKey(), myFbAppConfig.getSecret(), theSessionKey ) );
    }

    public IFacebookFacade getClientUsingAuthToken(String theAuthToken) {
	FacebookRestClient fbJsonRestClient = new FacebookRestClient( myFbAppConfig.getApiKey(), myFbAppConfig.getSecret() );
	try {
	    fbJsonRestClient.auth_getSession(theAuthToken);
	} 
	catch (Exception e) {
	    throw new RuntimeException( e );
	}
	return new FacebookFacadeRestClient( fbJsonRestClient );
    }

}
